﻿using System;
using System.Web.Configuration;
using System.Web.UI.WebControls;

namespace Salient.Web.Administration.Security.Wizard
{
    public partial class wizard : WizardPage
    {
        private const string COMPLETED_STEP = "WebAdminCompletedStep";
        private const string SELECTED_NODE = "WebAdminSelectedNode";

        private int CompletedStep
        {
            get
            {
                object obj = Page.Session[COMPLETED_STEP];
                return obj == null ? 0 : (int) obj;
            }
            set { Page.Session[COMPLETED_STEP] = value; }
        }

        private int SelectedNode
        {
            get
            {
                object obj = Session[SELECTED_NODE];
                return obj == null ? 0 : (int) obj;
            }
            set { Session[SELECTED_NODE] = value; }
        }

        public void ActiveViewChanged(object sender, EventArgs e)
        {
            UpdateControls();
        }

        public void CancelClick(object sender, EventArgs e)
        {
            Server.Transfer("../security.aspx");
        }

        public override void DisableWizardButtons()
        {
            nextButton.Enabled = false;
            prevButton.Enabled = false;
            cancelButton.Enabled = false;
            securityManagement.Visible = false;
        }

        public override void EnableWizardButtons()
        {
            UpdateControls();
            cancelButton.Enabled = true;
            securityManagement.Visible = true;
        }

        public string GetAltText(string altText)
        {
            return (string) GetGlobalResourceObject("GlobalResources", altText);
        }

        public bool IsWindowsAuth()
        {
            System.Configuration.Configuration config = OpenWebConfiguration(ApplicationPath);
            var auth = (AuthenticationSection) config.GetSection("system.web/authentication");
            return auth.Mode == AuthenticationMode.Windows;
        }

        // REVIEW: How to keep completed steps in sync with new selections.
        public void NavigationTreeNodeChanged(object sender, EventArgs e)
        {
            var tv = (TreeView) sender;
            int newSelectedIndex = Int32.Parse(tv.SelectedNode.Value);
            centralView.ActiveViewIndex = newSelectedIndex;
            SelectedNode = newSelectedIndex;
            UpdateControls();
            CompletedStep = newSelectedIndex - 1;
        }

        public void Page_Init()
        {
            // If returning after leaving a step that wasn't complete, set the correct active view.
            object activeView = Session["WizardActiveView"];
            if (activeView != null)
            {
                centralView.ActiveViewIndex = (int) activeView;
            }
            Session["WizardActiveView"] = null;

            string htmlText = "<td><img src=\"" + Request.ApplicationPath + "/images/yellowCORNER.gif\" width=\"34\"";
            htmlText = htmlText + " alt=\"" + (string) GetGlobalResourceObject("GlobalResources", "YellowCornerGif") +
                       "\"";
            htmlText = htmlText + " border=\"0\"/></td>";
            if (Directionality == "rtl")
            {
                yellowCornerElement.Text = "";
                yellowCornerElementBIDI.Text = htmlText;
            }
            else
            {
                yellowCornerElement.Text = htmlText;
                yellowCornerElementBIDI.Text = "";
            }

            UpdateControls();
        }

        public void NextStep(object sender, EventArgs e)
        {
            if (!((WebAdminUserControl) centralView.GetActiveView().Controls[1]).OnNext())
            {
                return;
            }
            if (!IsValid)
            {
                return;
            }

            // Review: design (perf etc).
            if (centralView.ActiveViewIndex == 1 && IsWindowsAuth())
            {
                centralView.ActiveViewIndex = 5;
                UpdateControls();
                return;
            }

            if (centralView.Views.Count - 1 == centralView.ActiveViewIndex)
            {
                Server.Transfer("../security.aspx");
            }

            centralView.ActiveViewIndex = centralView.ActiveViewIndex + 1;
            UpdateControls();
        }

        public void Page_Load()
        {
            nextButton.Focus();
        }

        public void PrevStep(object sender, EventArgs e)
        {
            if (!((WebAdminUserControl) centralView.GetActiveView().Controls[1]).OnPrevious())
            {
                return;
            }

            // Review: design (perf etc).
            if (centralView.ActiveViewIndex == 5 && IsWindowsAuth())
            {
                centralView.ActiveViewIndex = 1;
                UpdateControls();
                return;
            }

            centralView.ActiveViewIndex = centralView.ActiveViewIndex - 1;
            UpdateControls();
        }

        public override void SaveActiveView()
        {
            Session["WizardActiveView"] = centralView.ActiveViewIndex;
        }

        public void UpdateControls()
        {
            prevButton.Enabled = centralView.ActiveViewIndex > 0;
            navigationTree.Nodes[centralView.ActiveViewIndex].Selected = true;
            nextButton.Enabled = !(centralView.Views.Count - 1 == centralView.ActiveViewIndex);
        }
    }
}